package com.hunau.novel_reading.service.impl;

import com.hunau.novel_reading.common.ResultCodeEnum;
import com.hunau.novel_reading.entity.Users;
import com.hunau.novel_reading.exception.BusinessException;
import com.hunau.novel_reading.mapper.UsersMapper;
import com.hunau.novel_reading.request.UsersLoginReq;
import com.hunau.novel_reading.request.UsersRegisterReq;
import com.hunau.novel_reading.request.UsersSeekByUserId;
import com.hunau.novel_reading.request.UsersSeekReq;
import com.hunau.novel_reading.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UsersMapper usersMapper;
    @Override
    public Users Login(UsersLoginReq usersLoginReq) {
        Users users = usersMapper.selectByUsernameAndPassword(usersLoginReq.getUsername(), usersLoginReq.getPassword());
        if(users == null){
            throw new BusinessException(ResultCodeEnum.LOGIN_FAIL);
        }
        return users;
    }
    @Override
    public String register(UsersRegisterReq usersRegisterReq) {
        int count = usersMapper.insertUsers(usersRegisterReq.getUsername(), usersRegisterReq.getPassword());
        if(count <= 0){
            throw new BusinessException(ResultCodeEnum.USERNAME_EXISTS);
        }
        return "success";
    }
    @Override
    public String seek(UsersSeekReq usersSeekReq) {
        String password = usersMapper.selectPasswordByUsername(usersSeekReq.getUsername());
        if(password == null){
            throw new BusinessException(ResultCodeEnum.USERNAME_NOT_EXISTS);
        }
        return password;
    }
    @Override
    public Users selectByUserId(UsersSeekByUserId usersSeekByUserId) {
        System.out.println(usersSeekByUserId.getUserId());
        return usersMapper.selectByUserId(usersSeekByUserId.getUserId());
    }
}
